#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;

    std::vector<int> a(n);
    for (auto& x : a) {
        std::cin >> x;
    }
    a.insert(a.end(), a.begin(), a.end());

    std::vector<int> pos(m);
    std::iota(pos.rbegin(), pos.rend(), 2 * n);

    int ans = 0;
    for (int i = 2 * n - 2, cnt = m - 1; i >= 0; i--) {
        int v = a[i];
        if (v > 0 and pos[v - 1] < pos[v]) {
            cnt++;
        }
        if (v + 1 < m and pos[v] > pos[v + 1]) {
            cnt--;
        }
        if (i < n and cnt == 0) {
            ans++;
        }
        pos[v] = i;
    }
    std::cout << ans << "\n";

    return 0;
}
